#include "transferchange.h"
#include "ui_transferchange.h"
#include <QFileDialog>
#include "QNetworkRequest"
#include "QNetworkReply"
#include "QJsonParseError"
#include "QJsonArray"
#include "QJsonDocument"
#include "QJsonObject"
#include "QJsonValueRef"
#include <QDesktopServices>
#include "myclicklabel.h"

transferChange::transferChange(QWidget *parent, Person * m_user) :
    QWidget(parent),
    ui(new Ui::transferChange),
    transfer_user(m_user)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

transferChange::~transferChange()
{
    delete ui;
}

void transferChange::basicSettings()
{
    ui->labOriginal->setAlignment(Qt::AlignCenter);
    ui->labBackground->setAlignment(Qt::AlignCenter);
    ui->labResult->setAlignment(Qt::AlignCenter);

    //按钮初始不可点击
    ui->btnResult->setEnabled(false);
    ui->btnResult->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(128, 128, 128);}");
}

void transferChange::connectSettings()
{
    connect(ui->btnOriginal, &QPushButton::clicked, this, &transferChange::btnOriginal_clicked);
    connect(ui->btnBackground, &QPushButton::clicked, this, &transferChange::btnBackground_clicked);
    connect(ui->btnResult, &QPushButton::clicked, this, &transferChange::btnResult_clicked);
    connect(ui->labResult, &MyClickLabel::clicked, [=](){
        QDesktopServices::openUrl(QUrl(QString("file:///") + resultPath));
    });
}

void transferChange::btnOriginal_clicked()
{
    originalPath = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.jpg;*.png;*.jfif)");

    if(!originalPath.isNull())
    {
        ui->labOriginal->setPixmap(QPixmap(originalPath).scaled(ui->labOriginal->width(), ui->labOriginal->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    if(!originalPath.isNull() && !backgroundPath.isNull())
    {
        ui->btnResult->setEnabled(true);
        ui->btnResult->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
    }
}

void transferChange::btnBackground_clicked()
{
    backgroundPath = QFileDialog::getOpenFileName(this, "Open File", "E:\\Python\\FinalAIface\\test", "Image File(*.jpg;*.png;*.jfif)");

    if(!backgroundPath.isNull())
    {
        ui->labBackground->setPixmap(QPixmap(backgroundPath).scaled(ui->labBackground->width(), ui->labBackground->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    if(!originalPath.isNull() && !backgroundPath.isNull())
    {
        ui->btnResult->setEnabled(true);
        ui->btnResult->setStyleSheet("QPushButton{background: transparent;font: 13pt '黑体';color:rgb(240, 240, 240);border-radius:15px;background-color:rgb(255, 0, 0);}"
                                     "QPushButton:hover{background-color:rgb(220, 0, 0);}"
                                     "QPushButton:pressed{background-color:rgb(200, 0, 0);}");
    }
}

void transferChange::btnResult_clicked()
{
    //信号发送
    //resultPath = "E:\\Picture\\Ps\\miaomiao.jpg";

    //信号传递
    // URL
    QString baseUrl = "http://127.0.0.1:5000//StyleTransfer?Userid=" + transfer_user->getId() + "&Goal=" + backgroundPath + "&Origin=" + originalPath;
    QUrl url(baseUrl);

    qDebug() << baseUrl;

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    emit showWait();

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg(QNetworkReply*)));

}

void transferChange::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QJsonParseError error;
    QJsonDocument data = QJsonDocument::fromJson(array,&error);
    if(!data.isNull())
    {
        QJsonArray obj = data.array();
        QJsonObject object = obj[0].toObject();
        resultPath = object.value("path").toString();

        qDebug() << resultPath;

        if(!resultPath.isNull())
        {
            ui->labResult->setPixmap(QPixmap(resultPath).scaled(ui->labResult->width(), ui->labResult->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        }
    }
    emit closeWait();
}
